<template>
  <!-- 右侧栏 推荐 -->
  <div id="recommend" v-if="articles.length > 0">
    <el-divider content-position="left">
      推荐
    </el-divider>
    <el-row v-for="item in articles" :key="item.id">
      <el-card :body-style="{padding:'0px',margin:'0px'}">
        <el-col :span="10">
          <el-image :src="item.cover" fit="fill" class="recommend-item-image">
          </el-image>
        </el-col>
        <el-col :span="14">
          <div class="recommend-item-title">
            <el-link :underline="false" class="title" @click="showDetail(item.id)">
              {{ item.title }}
            </el-link>
          </div>
          <div class="rank-item-countScan">
            <i class="el-icon-view"></i>&nbsp;{{ item.countScan }}&nbsp;
            <i class="el-icon-time"></i>&nbsp;{{ item.createTime | formatTimer }}
          </div>
        </el-col>
      </el-card>
    </el-row>
  </div>
</template>

<script>
import {
    articleQueryList
} from '@/api/article'

export default {
    name: 'recommend',
    data () {
        return {
            articles: []
        }
    },
    filters: {
        formatTimer: function (value) {
            let date = new Date(value)
            let year = date.getFullYear()

            let month = date.getMonth() + 1
            month = month < 10 ? '0' + month : month

            let day = date.getDate()
            day = day < 10 ? '0' + day : day

            let hour = date.getHours()
            hour = hour < 10 ? '0' + hour : hour

            let minute = date.getMinutes()
            minute = minute < 10 ? '0' + minute : minute

            let second = date.getSeconds()
            second = second < 10 ? '0' + second : second

            console.log(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second)
            return year + '-' + month + '-' + day
        }
    },
    methods: {
        showDetail (articleId) {
            this.$router.push({
                path: 'articleDetail',
                query: {
                    'articleId': articleId
                }
            })
        }
    },
    mounted () {
        articleQueryList({
            isRecommend: true,
            limit: 10
        }).then((response) => {
            if (response && response.code === 200) {
                this.articles = response.data
            }
        })
    }
}
</script>

<style scoped>
#recommend {
  width: auto;
  height: 100%;
  margin: 10px 0px 0px 0px;
  padding: 5px;
  background-color: #fff;
}

.recommend {
  width: auto;
  height: 100%;
  margin: 0px;
  padding: 0px;
}

.recommend-item-image {
  width: 120px;
  height: 100%;
}

.recommend-item-title {
  width: auto;
  height: 40px;
}

.title {
  color: #333;
  font-size: 12.5px;
  height: 40px;
  line-height: 20px;
  width: 100%;
  margin: 0px 0px 0px 0px;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.rank-item-countScan {
  margin: 0px 0px 0px 0px;
  height: 30px;
  line-height: 30px;
}
</style>
